package com.wuwei.elearning.question.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.SelectRecord;
import com.wuwei.elearning.question.domain.UserRecord;
import com.wuwei.elearning.question.mapper.SelectRecordMapper;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户做的所有题目的答案的记录的表 前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-03-28
 */
@RestController
@RequestMapping("/elearning/selectRecord")
@CrossOrigin
public class SelectRecordController {

    @Resource
    private SelectRecordMapper selectRecordMapper;


    @PostMapping("updateOrAdd")
    public Result<String> updateOrAdd(@RequestBody SelectRecord selectRecord, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        LambdaQueryWrapper<SelectRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SelectRecord::getUserAccount,account).eq(SelectRecord::getSelectedId,selectRecord.getSelectedId());
        SelectRecord selectOne = selectRecordMapper.selectOne(queryWrapper);
        if (selectOne==null) {
            selectRecord.setUpdatedTime(LocalDateTime.now());
            selectRecord.setUserAccount(account);
            selectRecordMapper.insert(selectRecord);
        } else {
            selectOne.setUpdatedTime(LocalDateTime.now());
            selectOne.setSelected(selectRecord.getSelected());
            selectOne.setQuestionStatus(selectRecord.getQuestionStatus());
            selectRecordMapper.updateById(selectOne);
        }
        return Result.ok("用户选择答案信息更新成功！");
    }

    /**
     *
     * @param selectedId 如果小于10000，则为chapter表中的id字段，如果大于10000，则为subject表的id加上10000
     * @param request
     * @return
     */
    @GetMapping("getBySelectId/{selectedId}")
    public Result<Map<String,Object>> getByChapterId(@PathVariable("selectedId") Long selectedId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);

        LambdaQueryWrapper<SelectRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SelectRecord::getSelectedId,selectedId).eq(SelectRecord::getUserAccount,account);

        SelectRecord selectRecord = selectRecordMapper.selectOne(wrapper);

        Map<String,Object> result = new HashMap<>();

        if (selectRecord == null) {
//            result.put("result", null);
//            result.put("questionStatus",null);
//            result.put("length",0);
            return Result.ok(null);
        }

        String selected = selectRecord.getSelected();
        JSONArray array = JSON.parseArray(selected);
        result.put("result",selectRecord);
        result.put("length",array.size());
        return Result.ok(result);
    }

}

